package itheima.moc.TestServerSocketDemo8;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class Threadpool implements Runnable {
  private Socket acceptSocket;
    public Threadpool(Socket accept) {
        this.acceptSocket=accept;
    }
//    UUIDDemo uuidDemo = new UUIDDemo();
    @Override
    public void run() {
        BufferedOutputStream bos =null;
        try {
            BufferedInputStream bis = new BufferedInputStream(acceptSocket.getInputStream());
            //调用方法UUID可以使文件名唯一
            bos = new BufferedOutputStream(new FileOutputStream("Day15\\Server"+ UUID.randomUUID().toString()+".jpg"));

            int b;
            while ((b=bis.read())!=-1){
                bos.write((char)b);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(acceptSocket.getOutputStream()));
            bw.write("利好哦 熊迪");
            bw.newLine();
            bw.flush();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //需要进行非空判断
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
           if(acceptSocket!=null){
               try {
                   acceptSocket.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
        }
    }
}
